package br.com.ia.controle;

import br.com.ia.bean.Host;
import br.com.ia.bean.Rede;
import java.util.Iterator;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.UIManager;

/**
 *
 * @author Jonas Cezar
 */
public class JFPrincipal extends javax.swing.JFrame {

    private Rede rede;
    private GerenciaArquivos arquivos;
    private Buscas buscas;

    /** Creates new form JFPrincipal */
    public JFPrincipal() {
        initComponents();

        arquivos = new GerenciaArquivos();
    }

    public void selecionaFile() {

        JFileChooser fc = new JFileChooser();
        fc.addChoosableFileFilter(new MyFilter("txt"));
        fc.setAcceptAllFileFilterUsed(false);
        fc.setDialogTitle("Selecione uma Video. '.txt'");
        fc.setApproveButtonText("Abrir");

        int returnVal = fc.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {

            try {

                rede = arquivos.leAquivo(fc.getSelectedFile());

                inicializaCampos();

            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
    }

    public void inicializaCampos() {

        if (rede != null) {

            new GeraGrafo().gerar(rede);

            if (buscas == null) {
                buscas = new Buscas();
                new Thread(buscas).start();
            }

            buscas.setHosts(rede.getHosts());

            Iterator i = rede.getHosts().keySet().iterator();

            while (i.hasNext()) {

                String key = (String) i.next();

                jComboBoxNodos1.addItem(key);
                jComboBoxNodos2.addItem(key);
            }
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButtonLerArquivo = new javax.swing.JButton();
        jComboBoxNodos1 = new javax.swing.JComboBox();
        jComboBoxNodos2 = new javax.swing.JComboBox();
        jButtonEmProfundidade = new javax.swing.JButton();
        jButtonEmLargura = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaResultadoProfundidade = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaResultadoLargura = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Trabalho IA");
        setResizable(false);

        jPanel1.setToolTipText("Tela Principal");
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados"));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButtonLerArquivo.setText("Ler Arquivo");
        jButtonLerArquivo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLerArquivoActionPerformed(evt);
            }
        });
        jPanel3.add(jButtonLerArquivo, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 20, -1, -1));

        jPanel3.add(jComboBoxNodos1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 100, -1));

        jPanel3.add(jComboBoxNodos2, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 70, 100, -1));

        jButtonEmProfundidade.setText("Busca em Profundidade");
        jButtonEmProfundidade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEmProfundidadeActionPerformed(evt);
            }
        });
        jPanel3.add(jButtonEmProfundidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 120, 200, -1));

        jButtonEmLargura.setText("Busca em Largura");
        jButtonEmLargura.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEmLarguraActionPerformed(evt);
            }
        });
        jPanel3.add(jButtonEmLargura, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 120, 200, -1));

        jTextAreaResultadoProfundidade.setColumns(20);
        jTextAreaResultadoProfundidade.setRows(5);
        jScrollPane1.setViewportView(jTextAreaResultadoProfundidade);

        jPanel3.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 160, 240, 370));

        jTextAreaResultadoLargura.setColumns(20);
        jTextAreaResultadoLargura.setRows(5);
        jScrollPane2.setViewportView(jTextAreaResultadoLargura);

        jPanel3.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, 240, 370));

        jPanel1.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 520, 540));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonEmLarguraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEmLarguraActionPerformed

        if (buscas != null && jComboBoxNodos1.getSelectedItem() != null
                && jComboBoxNodos2.getSelectedItem() != null) {

            buscas.buscaEmLargura(new Host(String.valueOf(jComboBoxNodos1.getSelectedItem()), ""), new Host(String.valueOf(jComboBoxNodos2.getSelectedItem()), ""), jTextAreaResultadoLargura);

        }

    }//GEN-LAST:event_jButtonEmLarguraActionPerformed

    private void jButtonEmProfundidadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEmProfundidadeActionPerformed

        if (buscas != null && jComboBoxNodos1.getSelectedItem() != null
                && jComboBoxNodos2.getSelectedItem() != null) {

            buscas.buscaEmProfundidade(new Host(String.valueOf(jComboBoxNodos1.getSelectedItem()), ""), new Host(String.valueOf(jComboBoxNodos2.getSelectedItem()), ""), jTextAreaResultadoProfundidade);
        }

    }//GEN-LAST:event_jButtonEmProfundidadeActionPerformed

    private void jButtonLerArquivoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLerArquivoActionPerformed

        selecionaFile();

    }//GEN-LAST:event_jButtonLerArquivoActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                try {

                    javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }

                JFPrincipal principal = new JFPrincipal();
                principal.setLocationRelativeTo(null);
                principal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                principal.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonEmLargura;
    private javax.swing.JButton jButtonEmProfundidade;
    private javax.swing.JButton jButtonLerArquivo;
    private javax.swing.JComboBox jComboBoxNodos1;
    private javax.swing.JComboBox jComboBoxNodos2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaResultadoLargura;
    private javax.swing.JTextArea jTextAreaResultadoProfundidade;
    // End of variables declaration//GEN-END:variables
}
